/**
 * Created by Administrator on 2017/12/4.
 */
import React from 'react';
import $ from 'jquery'
import WxServer from '../../wx/WxServer'
import {Row, Col} from 'antd';
import Link from '../common/Link'
import Popup from '../common/popup'
import './style/business.scss'
import banner from './img/banner.png'
import qiye from './img/qiye.png'
import spgl from './img/spgl.png'
import xfyz from './img/xfyz.png'
import ddgl from './img/ddgl.png'
import fail from './img/fail.png'
import success from './img/success.png'

import {
    HttpService,
    Toast
} from '../../utils'

export  default  class Test extends React.Component {
    constructor(props) {
        super(props);
        this.companyName=''
        this.state = {
            flag: false,
            hasCreateBusiness: false,
            needAudit: false,  //重新编辑状态
        }
        this.needAudit='';
        this.config = {
            isSure: false,
            isCancel: false,
            no: '取消',
            yes: '确定',
            content: '<p style="margin: 30px">重新编辑将会撤销审核，请编辑后重新提交审核</p>',
            yes_cb: () => {
                this.context.router.push('/businessForm/reAudit')
            },
            no_cb: () => {
                this.setState({
                    flag: false
                });
            }
        };
        this.createBuiness = this.createBuiness.bind(this)
    }

    static contextTypes = {
        router: React.PropTypes.object.isRequired
    };

    componentWillMount(){
        var rec=this;
        WxServer.init()
        var btoken=localStorage.btoken;
        // let hasCreateBusiness=this.props.params.company;
        // if(hasCreateBusiness!='null'){
        //     rec.setState({
        //         hasCreateBusiness:true
        //     })
        // }

        $.ajax({
            url: '/jf/business/company/getByCompanyUser',
            async:false,
            data: {
                token:btoken
            },
            success:function(res){
                if(res.code=='10140'){
                    if(res.data){
                        localStorage.companyId=res.data.id;
                        rec.companyName=res.data.name
                        rec.needAudit=res.data.auditState;
                        rec.setState({
                            needAudit:res.data.auditState
                        })
                    }
                }
            }
        });

    }
    componentDidMount() {


    }

    //创建商户按钮点击事件
    createBuiness(event) {
        this.context.router.push('/businessForm')
    }


    //重新编辑按钮点击事件
    reAudit() {
        let auditState=this.needAudit;
        if(auditState==1){
            this.setState({
                flag:true
            })
        }else{
            this.context.router.push('/businessForm/reAudit')
        }
     }

    auditBox() {
        let auditState=this.needAudit;
        switch (auditState) {
            case 1:
                return <div className="failInfo">
                    <img src={success}/>
                    <div className="info">商户审核中</div>
                    <button className="btn Btn" onClick={this.reAudit.bind(this)}>重新编辑</button>
                </div>
            case 2:
                return <div className="menuBox">
                    <h4 className="welcome">您好，{this.companyName}！</h4>
                    <div className="enter">
                        <Row>
                            <Col xs={{span: 8}} className="enter-col">
                                <Link className="box1" to={'/goodsManage/'}>
                                    <img src={spgl}/>
                                    <span> 商品管理</span>
                                </Link>
                            </Col>
                            <Col xs={{span: 8}} className="enter-col">
                                <Link className="box1" to={'/orderManage/'}>
                                    <img src={ddgl}/>
                                    <span> 订单管理</span>
                                </Link>
                            </Col>
                            <Col xs={{span: 8}} className="enter-col">
                                <Link className="box1" to={'/companyData/'}>
                                    <img src={qiye}/>
                                    <span> 企业资料</span>
                                </Link>
                            </Col>
                            <Col xs={{span: 8}} className="enter-col">
                                <Link className="box1" to={"/numberVerify"}>
                                    <img src={xfyz}/>
                                    <span> 消费验证</span>
                                </Link>
                            </Col>
                        </Row>
                    </div>

                </div>
            case 3:
                return <div className="failInfo">
                    <img src={fail}/>
                    <div className="info" style={{color:'#ff5b57'}}>您的商户未通过审核</div>
                    <button className="btn Btn" onClick={this.reAudit.bind(this)}>重新编辑</button>
                </div>
            default:
                return (<div className="failInfo">
                    <img src={fail}/>
                    <div className="info">您当前还没有商户</div>
                    <div>请先创建商户</div>
                    <button className="btn Btn" onClick={this.createBuiness.bind(this)}>创建商户</button>
                </div>)
        }
    }

    logout(){
        localStorage.removeItem('btoken');
        localStorage.removeItem('companyId');
        this.context.router.push('/businesslogin')
    }

    render() {
        const {hasCreateBusiness, needAudit, flag} = this.state;
        let auditBox=this.auditBox();

        return (
            <div>
                <Popup config={this.config} blockOrNone={flag} _flag={flag}/>
                <div className="buinessIndex">
                    <div className="header">
                        <img src={banner}/>
                    </div>
                    {auditBox}
                    <button onClick={this.logout.bind(this)}  className="btn Btn logoutBtn">退出登录</button>
                </div>

            </div>

        )
    }
}